function stopDault(e)
{
    if (e && e.preventDefalut)
        e.preventDefalut();
    else
        window.event.retuenValue = false;
}
function callWechatPay(params, success, fail, cancel) {
    WeixinJSBridge.invoke('getBrandWCPayRequest',params,function(res){
        //alert(res.err_msg);
        if(res.err_msg === "get_brand_wcpay_request:ok" ) {
            if(success){
                success(res);
            }
        }
        if(res.err_msg === "get_brand_wcpay_request:cancel" ) {
            if(cancel){
                cancel(res);
            }
        }
        if(res.err_msg === "get_brand_wcpay_request:fail" ) {
            if(fail){
                fail(res);
            }
        }
        // 使用以上方式判断前端返回,微信团队郑重提示:res.err_msg 将在用户支付成功后返
        //回 ok,但幵丌保证它绝对可靠。
    });
}

function onDocumentReachBottom(callback) {
    $(window).scroll(function() {
        var totalheight = parseFloat($(window).height()) + parseFloat($(document).scrollTop());
        if ($(document).height() <= totalheight){
            if (callback) callback();
        }
    });
}

function closeOrder(order_id, callback) {
    var closeReason = '', loading;
    function sendRequest(){
        $.ajax({
            type:'POST',
            url:'/h5/bought/cancel',
            data:{order_id:order_id, closeReason:closeReason},
            beforeSend:function(){
                loading = weui.loading('订单处理中');
            },
            success:function (response) {
                setTimeout(function () {
                    loading.hide();
                    if (callback) callback(response);
                }, 500);
            }
        });
    }
    weui.actionSheet([
        {
            label:'买错了重新拍',
            onClick:function () {
                closeReason = '买错了重新拍';
                sendRequest();
            }
        },
        {
            label:'我不想买了',
            onClick:function () {
                closeReason = '我不想买了';
                sendRequest();
            }
        }
    ],
        [
            {
                label:'取消',
                onClick:function () {

                }
            }
        ]);
}

function showShipping(order_id) {
    $.ajax({
        url:'/h5/bought/getshipping',
        data:{order_id:order_id},
        success:function (response) {
            if (response.errcode)
            {
                weui.toast(response.errmsg);
            } else {
                var overlayer = $("<div/>").addClass('dsxui-overlayer').appendTo(document.body);
                var dialog = $("<div/>").addClass('shipping-dialog').appendTo(document.body);
                var title = '<div class="dialog-title">\n' +
                    '                <strong>'+response.express_name+'</strong>\n' +
                    '                <span>'+response.express_no+'</span>\n' +
                    '            </div>';

                var contents = '';
                $(response.traces).each(function (i, data) {
                    contents+= '<div class="row-item">\n' +
                        '                    <div class="time">'+data.AcceptTime+'</div>\n' +
                        '                    <div class="flex">'+data.AcceptStation+'</div>\n' +
                        '                </div>';
                });
                contents = '<div class="dialog-content">'+contents+'</div>';
                dialog.append('<div class="dialog-body">'+title+contents+'</div>').center();
                $("body").css({'overflow':'hidden'});

                dialog.on('tap', function (e) {
                    DSXUtil.stopPropagation(e);
                });

                overlayer.on('click', function () {
                    $("body").css({'overflow':'auto'});
                    overlayer.remove();
                    dialog.remove();
                });
            }
        }
    });
}

function deleteOrder(order_id, callback) {
    weui.confirm('确定要删除此订单吗?', function () {
        $.ajax({
            url:'/h5/bought/delete',
            data:{order_id:order_id},
            success:function (response) {
                if (callback) callback(response);
            }
        });
    });
}

function payOrder(order_id, callback) {
    $.ajax({
        url:'/h5/pay/getconfig',
        data:{order_id:order_id},
        success:function (response) {
            callWechatPay(JSON.parse(response.config), function (res) {
                if (callback) callback(res);
            }, function (err) {
                weui.toast(err.msg);
            }, function (res) {
                weui.toast('付款已取消');
            });
        }
    });
}

function confirmOrder(order_id, callback) {
    weui.confirm('请确认你已收到货物，以免人财两空?', function () {
        var loading;
        $.ajax({
            url:'/h5/bought/confirm',
            data:{order_id:order_id},
            beforeSend:function(){
                loading = weui.loading('订单处理中');
            },
            success:function (response) {
                setTimeout(function () {
                    loading.hide();
                    if (callback) callback(response);
                }, 500);
            },
            complete:function (xhr) {
                //alert(JSON.stringify(xhr));
            }
        });
    });
}

function noticeOrder(order_id) {
    $.ajax({
        url:'/h5/bought/notice',
        data:{order_id:order_id},
        success:function (response) {
            weui.toast('已提醒卖家发货');
        }
    });
}

function evaluteOrder(order_id, callback) {
    var starsnum = 1;
    var textarea = '<textarea id="evalute-msg" class="weui-textarea" rows="5" placeholder="请发表你对本次购物的体验和看法" style="background-color: #fafafa;"></textarea>';
    var stars = '<div class="evalute-stars flex">' +
        '<div class="iconfont icon-favor_fill_light"></div>' +
        '<div class="iconfont icon-favor_light"></div>' +
        '<div class="iconfont icon-favor_light"></div>' +
        '<div class="iconfont icon-favor_light"></div>' +
        '<div class="iconfont icon-favor_light"></div>' +
        '</div>';
    var evalute = textarea+'<div class="display-flex"><div class="evalute-tips">星级评分:</div>'+stars+'</div>';
    weui.confirm(evalute, function () {
        $.ajax({
            type:'POST',
            url:'/h5/bought/evalute',
            data:{order_id:order_id, stars:starsnum, message:$('#evalute-msg').val()},
            success:function (response) {
                weui.toast('评价成功');
                if (callback) callback(response);
            },
            complete:function (xhr) {
                //alert(JSON.stringify(xhr));
            }
        });
    });
    $(".evalute-stars .iconfont").on('click', function () {
        var index = $(this).index() + 1;
        starsnum = index;
        $(".evalute-stars .iconfont").each(function (i, el) {
            if (i < index) {
                $(el).removeClass('icon-favor_light').addClass('icon-favor_fill_light');
            } else {
                $(el).removeClass('icon-favor_fill_light').addClass('icon-favor_light');
            }
        });
    });
}

function showLicense() {
    $.ajax({
        url:'/h5/getlicense',
        success:function (response) {
            var overlayer = $('<div/>').addClass('dsxui-overlayer').appendTo(document.body);
            var license = $("<div/>").addClass('license').appendTo(document.body);
            license.html('<div class="content">\n' +
                '            <div class="head">证品荟商城服务协议</div>\n' +
                '            <div class="body">'+response.license+'</div>\n' +
                '            <div class="btn" id="hideLicense">我已阅读并同意《证品荟用户协议》</div>\n' +
                '        </div>').center();

            $("#hideLicense").on('click', function () {
                overlayer.remove();
                license.remove();
            });
        }
    });
}
